package com.unitd.frame.springweb.controller.result;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;

/**
 * @desc 自定义的controller向前端返回分页数据的数据类型类, 用于统一管理返回分页数据信息
 * @filename PageResult.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2017/4/1
 */
public class PageResult implements Serializable {
	/** 成功与否表示(true:成功; false:失败) */
	private boolean success;
	/**
	 * 接口返回码, 0表示成功, 其他看对应的定义
	 * 推荐的做法是: 0: 成功; >0:表示已知的异常(例如提示错误等, 需要调用地方单独处理);<0:表示未知的异常(不需要单独处理, 调用方统一处理)
	 */
	private int code;
	/** 返回的信息(主要出错的时候使用) */
	@JsonInclude(JsonInclude.Include.NON_NULL)
	private String msg;
	/** 数据记录条数 */
	private Long count;
	/** 返回的数据 */
	@JsonInclude(JsonInclude.Include.NON_NULL)
	private Object data;

	/* ************** 定义返回代码常量 ***************/
	/** 处理成功 */
	private static final int SUCCESS = 0;
	/** 处理失败 */
	public static final int FAIL = -1;

	public PageResult() {
		super();
	}

	public PageResult(Throwable e) {
		this(FAIL, false, e.getMessage(), 0L, null);
	}

	public PageResult(Long count, Object data) {
		this(SUCCESS, true, null, count, data);
	}

	public PageResult(String msg, Long count, Object data) {
		this(SUCCESS, true, msg, count, data);
	}

	public PageResult(int code, boolean success, String msg, Long count, Object data) {
		super();
		this.code = code;
		this.success = success;
		this.msg = msg;
		this.count = count;
		this.data = data;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Long getCount() {
		return count;
	}

	public void setCount(Long count) {
		this.count = count;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}
}